// pages/scenicPay/scenicPay.js
var network = require("../../utils/ajax.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ open_id: options.open_id })
    this.onClick_pay(options.orderID)
  },
  onClick_pay(orderID) {
    let that = this;
    let open_id = this.data.open_id
    network.POST({
      API_URL: "api/OpenUserOrder/WeChatPay?OpenID=" + open_id + "&orderId=" + orderID + "",
      success: (res) => {
        let {
          timeStamp,
          nonceStr,
          signType,
          paySign
        } = res.data.Data;
        let packages = res.data.Data.package;
        //微信支付
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: packages,
          signType: signType,
          paySign: paySign,
          success: function (res) {
            //console.log(res);
            wx.showToast({
              title: '支付成功', //提示的内容,
              icon: 'success', //图标,
              duration: 1000,
              complete: () => {
                if (that.data.productCardId) {
                  wx.navigateBack({
                    delta: 2
                  })
                }
                wx.redirectTo({
                  url: '../web/web?url=' + encodeURIComponent('https://zlhwh5.1lianyou.com/pages/ticket_order_details_H5/ticket_order_details_H5?id=' + orderID),
                })
              }
            });
          },
          fail: function (res) {
            if (that.data.productCardId) {
              wx.showToast({
                title: '支付失败', //提示的内容,
                icon: 'none', //图标,
                duration: 1500
              })
              wx.navigateBack({
                detail: 2
              })
              return;
            }
            wx.showToast({
              title: '支付失败，即将跳转订单详情', //提示的内容,
              icon: 'none', //图标,
              duration: 1500
            })
            //console.log('跳转')
            setTimeout(() => {
              wx.redirectTo({
                url: '../web/web?url=' + encodeURIComponent('https://zlhwh5.1lianyou.com/pages/ticket_order_details_H5/ticket_order_details_H5?id=' + orderID),
              })
            }, 1500)
          },
        });
      },
      fail: function (res) {
        console.error(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index'
    }
  }
})